from pyquery import PyQuery as pq
from utils.http_client import Http_Clien as http
from javdb import config


class PageParse:
    domain_url = config['domain']['javdb_url']

    @classmethod
    def get_data(cls, response):
        doc = pq(response)
        # 需要确认是否满18岁
        adult_age_confirm_url = cls.domain_url + doc("a:contains('是,我已滿18歲')").attr("href")  # 获取确认url
        response = http.single_get(adult_age_confirm_url) if adult_age_confirm_url != None else response
        doc = pq(response)
        # 获取影片标题
        title = doc("h2>strong").text()
        # 番号获取
        movie_id = doc("div.panel-block.first-block").text().split("番號:")[1].strip()
        # 磁力链接
        url_has_HD = doc("td.magnet-name a[href*='HD']").attr('href')
        span_has_HD = doc('td.magnet-name span:contains("HD")').parent().attr("href")
        span_has_gaoqing = doc("td.magnet-name span:contains('高清')").parent().attr("href")
        if url_has_HD != None:
            magnets_url = url_has_HD
        elif span_has_HD != None:
            magnets_url = url_has_HD
        elif span_has_gaoqing != None:
            magnets_url = span_has_gaoqing
        else:
            magnets_url = None

        if magnets_url != None:
            return {"title": title, "movie_id": movie_id, "magnets_url": magnets_url}
